package models

import (
	"database/sql"
	"fmt"
	"github.com/edwingeng/wuid/mysql/wuid"
)

var g *wuid.WUID

func initWuid(dsn string) {
	newDbB := func() (*sql.DB, bool, error) {
		db, _ := connect(dsn)
		return db, true, nil
	}
	g = wuid.NewWUID("default", nil)
	_ = g.LoadH28FromMysql(newDbB, "wuid")

	//for i := 0; i < 100; i++ {
	//	fmt.Printf("%#016x \n", g.Next())
	//}
}

func WUID() string {
	return fmt.Sprintf("%#016x", g.Next())
}

func connect(dsn string) (*sql.DB, error) {
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		return nil, err
	}
	return db, nil
}
